<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox-content">
					<form class="form-horizontal m-t" id="signupForm">
						<input id="id" name="id" th:value="${user.id}" type="hidden">
						<input type="hidden" name="roleIds" id="roleIds" th:value="${roleIds}">
						<div class="form-group">
							<label class="col-sm-3 control-label">角色</label>
							<div class="col-sm-8">
								<label th:each="role:${roles}" class="checkbox-inline hidden" th:attrprepend="class=${role.roletype+' '}"  th:attrappend="class=${' ' + role.rolefrom}">
									<input name="role" type="checkbox" th:value="${role.id}" th:text="${role.roleName}" th:checked="${role.roleSign}">
								</label>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-8 col-sm-offset-3">
								<button type="button" class="btn btn-primary" onclick="update();">提交</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div>
			<span id="loginFrom" class="hidden"><shiro:principal property="loginFrom"/></span>
			<script type="text/javascript">
                var sjpriv = false;
                var qjpriv = false;
                var xjpriv = false;
			</script>
		</div>
		<div shiro:hasPermission="sys:sjpriv">
			<script type="text/javascript">
                sjpriv = true;
			</script>
		</div>
		<div shiro:hasPermission="sys:qjpriv">
			<script type="text/javascript">
                qjpriv = true;
			</script>
		</div>
		<div shiro:hasPermission="sys:xjpriv">
			<script type="text/javascript">
                xjpriv = true;
			</script>
		</div>
	</div>
	<div th:include="include::footer"></div>
	<script type="text/javascript">
		var loginFrom ="."+$('#loginFrom').text();
        // 以下为官方示例
        $().ready(function() {
            if(sjpriv){
				$(loginFrom+'.sj').removeClass('hidden');
                $(loginFrom+'.qj').removeClass('hidden');
                $(loginFrom+'.xj').removeClass('hidden');
			}
			if(qjpriv){
                $(loginFrom+'.qj').removeClass('hidden');
                $(loginFrom+'.xj').removeClass('hidden');
			}
			if(xjpriv){
                $(loginFrom+'.xj').removeClass('hidden');
			}
        });
        function update() {
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache : true,
                type : "POST",
                url : "/sys/user/empower",
                data : $('#signupForm').serialize(),// 你的formid
                async : false,
                error : function(request) {
                    alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg(data.msg);
                        $(window.parent.document).find('.J_iframe').each(function () {
                            if ($(this).data('id') == "/sys/user") {//common/sysDict为打开的frame id
                                this.contentWindow.reLoad();
                                return false;
                            }
                        });
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);

                    } else {
                        parent.layer.msg(data.msg);
                    }

                }
            });

        }
        function getCheckedRoles() {
            var adIds = "";
            $("input:checkbox[name=role]:checked").each(function(i) {
                if (0 == i) {
                    adIds = $(this).val();
                } else {
                    adIds += ("," + $(this).val());
                }
            });
            return adIds;
        }
        function setCheckedRoles() {
            var roleIds = $("#roleIds").val();
            alert(roleIds);
            var adIds = "";
            $("input:checkbox[name=role]:checked").each(function(i) {
                if (0 == i) {
                    adIds = $(this).val();
                } else {
                    adIds += ("," + $(this).val());
                }
            });
            return adIds;
        }
	</script>
</body>
</html>
